package com.c.ctgproject.ui.review.part

import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.R
import com.c.ctgproject.adapter.review.PartsChildAdapter
import com.c.ctgproject.base.BaseActivity
import com.c.ctgproject.entity.review.PartReviewEntity
import com.c.ctgproject.mode.MyConstants
import com.c.ctgproject.mode.ReviewEnum
import com.c.ctgproject.utils.MyLog
import com.c.ctgproject.utils.handlerNull
import com.c.ctgproject.utils.initData
import com.c.ctgproject.utils.showToast
import com.c.ctgproject.wiget.dialog.InputDialog
import kotlinx.android.synthetic.main.activity_part_order_detail_atciivty.*
import org.jetbrains.anko.textColorResource

/**
 *作者:LiBW
 *创建日期: 2020/9/9
 *描述: 配件销售详情 配件销售单
 */
class PartOrderDetailActivity : BaseActivity(), InputDialog.InPutDialogCallBack {
    private val mViewModel: PartOrderDetailViewModel by lazy { ViewModelProvider(this)[PartOrderDetailViewModel::class.java] }
    private val dialog: InputDialog by lazy { InputDialog(this) }

    private val data: PartReviewEntity.DataBean.ListBean by lazy { intent.extras!!.get(MyConstants.ENTITY) as PartReviewEntity.DataBean.ListBean }
    private val pageType: Int by lazy { intent.getIntExtra(MyConstants.AGREE_TYPE, 100) }
    private var type = 0
    override fun getLayout() = R.layout.activity_part_order_detail_atciivty
    override fun event() {
        changeTransparentStatusBar()
        changeStatusIconColor(true)
        when (pageType) {
            //未审核
            MyConstants.UN_REVIEWED -> {
                mButtonLL.visibility = View.VISIBLE
                mBottomCard.visibility = View.GONE
            }
            //已审核
            MyConstants.AGREE -> {
                mButtonLL.visibility = View.GONE
                mBottomCard.visibility = View.VISIBLE
                mReviewStatusTV.textColorResource = R.color.green_doorkeeper
            }
            //已拒绝
            MyConstants.REFUSE -> {
                mButtonLL.visibility = View.GONE
                mBottomCard.visibility = View.VISIBLE
                mReviewStatusTV.textColorResource = R.color.red
            }
            else -> {

            }
        }

        //是否为提供给销售页面的展示页面(不提供按钮功能)
        val  showType=intent.getBooleanExtra(MyConstants.JUST_SHOW,false)
        if (showType){
            mButtonLL.visibility = View.GONE
        }

        //控件赋值
        mBuyerTV.text = data.purchaser.handlerNull()
        mPhoneNumTV.text = data.phoneNumber.handlerNull()
        mCarCodeTV.text = data.carCode.handlerNull()
        mCarBrandTV.text = data.carBrand
        when (data.isPass) {
            ReviewEnum.UN_REVIEWED.code -> {
                mReviewStatusTV.text = ReviewEnum.UN_REVIEWED.statueName
                mReviewStatusTV.textColorResource = ReviewEnum.UN_REVIEWED.color

            }
            ReviewEnum.AGREE.code -> {
                mReviewStatusTV.text = ReviewEnum.AGREE.statueName
                mReviewStatusTV.textColorResource = ReviewEnum.AGREE.color

            }
            ReviewEnum.REFUSE.code -> {
                mReviewStatusTV.text = ReviewEnum.REFUSE.statueName
                mReviewStatusTV.textColorResource = ReviewEnum.REFUSE.color
            }
        }
        mRemarkTV.text = data.auditRemark.toString()
        val list = data.partsInfos
        MyLog.d("${data.partsInfos?.size}")
        list?.let {
            MyLog.d("一共有多少数据:${it.size}")
            val mAdapter = PartsChildAdapter(this, list)
            mRecyclerView.adapter = mAdapter
            mRecyclerView.layoutManager = LinearLayoutManager(this)
        }

        //确认订单回调
        mViewModel.confirmOrder.observe(this, Observer {
            loading.dismiss()
            MyLog.d("订单确认回调:$it")
            val data = initData(it)
            if (data.status == 0) {
                showToast("提交成功")
                finish()
            } else {
                showToast(data.msg)
            }
        })

        mViewModel.error.observe(this, Observer {
            loading.dismiss()
            showToast(it)
        })
    }

    override fun click() {
        //同意
        mConfirmRL.setOnClickListener {
            if (data.partsSalesOrderId.toString().isEmpty()) {
                showToast("订单ID为空无法提交")
                return@setOnClickListener
            }
            dialog.show(supportFragmentManager, "dialog")
            type = MyConstants.AGREE
        }
        //拒绝
        mCancelBTN.setOnClickListener {
            if (data.partsSalesOrderId.toString().isEmpty()) {
                showToast("订单ID为空无法提交")
                return@setOnClickListener
            }
            type = MyConstants.REFUSE
            dialog.show(supportFragmentManager, "dialog")
        }
    }

    override fun dialogResultText(msg: String) {
        loading.show()
        dialog.dismiss()
        mViewModel.orderConfirm(msg, type, data.partsSalesOrderId)
        MyLog.d("输入的内容$msg")
    }
}